Imports System.Data
Imports System.Data.SqlClient
Imports System.Net.Mail

Partial Class Admin_EventEmail
    Inherits System.Web.UI.Page

    Private strPath As String = Server.MapPath("~\Files") & "\"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            GetEventDetails()
        End If
    End Sub

    Protected Sub GetEventDetails()

        Dim dv As DataView = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)

        For Each dr As DataRow In dv.Table.Rows
            titleTextBox.Text = dr("title".ToString)
        Next
    End Sub

    Property EventID() As Integer
        Get
            Dim m_EventID As Integer
            Dim id As Object = ViewState("EventID")
            If Not id Is Nothing AndAlso IsNumeric(id) Then
                m_EventID = CInt(id)
            Else
                id = Request.QueryString("EventID")
                If Not id Is Nothing AndAlso IsNumeric(id) Then
                    m_EventID = CInt(id)
                Else
                    m_EventID = 1
                End If

                ViewState("EventID") = m_EventID
            End If
            Return m_EventID
        End Get
        Set(ByVal value As Integer)
            ViewState("EventID") = value
        End Set
    End Property

    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        If String.IsNullOrEmpty(AttachmentFile.FileName) OrElse AttachmentFile.PostedFile Is Nothing Then
        Else
            fileUpload_Click(Me, System.EventArgs.Empty)
        End If

        Dim haveAttachment As Boolean = False
        Dim strFiles() As String = Nothing

        If txtAttachments.Text.Length > 0 Then
            strFiles = Split(txtAttachments.Text, ControlChars.NewLine)
            haveAttachment = True
        End If

        Dim ToAddresses As MailAddressCollection = GetRecipients()

        Dim mem As MembershipUser = Membership.GetUser()
        Dim gui As New Guid(mem.ProviderUserKey.ToString)
        Dim name As String = GetName(gui)
        Dim msgFrom As New MailAddress(mem.Email, name)
        Dim msgMail As New MailMessage()
        msgMail.From = msgFrom

        For Each msgTo As MailAddress In ToAddresses
            msgMail.To.Add(msgTo)
        Next

        msgMail.Subject = txtSubject.Text

        msgMail.IsBodyHtml = True
        msgMail.Body = FCKeditor1.Value

        If haveAttachment Then
            Dim str As String
            For Each str In strFiles
                If str = "" Then Continue For
                msgMail.Attachments.Add(New Attachment(strPath & str))
            Next
        End If

        Dim smtp As New SmtpClient
        smtp.Send(msgMail)

        Response.Redirect("~/Events/View.aspx?eventid=" & EventID)
    End Sub

    Private Function GetName(ByVal userid As Guid) As String
        Return ClubStarterKit.Web.Members.FirstAndLastName(userid)
    End Function

    Protected Function GetRecipients() As MailAddressCollection
        Dim retval As New MailAddressCollection
        Dim mem As MembershipUser

        Dim dv As DataView = CType(SqlDataSource2.Select(DataSourceSelectArguments.Empty), DataView)

        For Each dr As DataRow In dv.Table.Rows
            mem = Membership.GetUser(dr("UserId"))
            Dim gui As New Guid(mem.ProviderUserKey.ToString)
            Dim name As String = GetName(gui)
            Dim address As New MailAddress(mem.Email, name)
            retval.Add(address)
        Next
        Return retval
    End Function


    Protected Sub fileUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles fileUpload.Click
        If String.IsNullOrEmpty(AttachmentFile.FileName) OrElse AttachmentFile.PostedFile Is Nothing Then
            Return
        Else
            Dim strFileName As String = AttachmentFile.FileName
            Dim c As String = System.IO.Path.GetFileName(strFileName)
            Try

                txtAttachments.Visible = True
                AttachmentFile.PostedFile.SaveAs(strPath & c)
                txtAttachments.Text &= c & ControlChars.NewLine
            Catch Exp As Exception
                Throw Exp
            End Try
        End If
    End Sub
End Class
